<?php
/**
 * 分页处理类
 * @author prg
 *
 */
class PageList
{
	private $page_items;
	private $cur_page;
	private $all_pages;
	private $all_items = array();
	private $return_items = array();
	
	/**
	 * 
	 * @param int $cur_page
	 * @param array $all_itmes
	 */
	function __construct($cur_page=1,$all_itmes=null)
	{
		$this->page_items=3; //每页显示5条
		$this->cur_page  =$cur_page;
		$this->all_items =$all_itmes;
		$this->all_pages = intval(ceil(count($all_itmes)/$this->page_items));
	}
	
	/**
	 * 返回系显示的数目
	 * @return multitype:
	 */
	function returnItems()
	{
		$cur = $this->cur_page-1;
		$all_items = count($this->all_items);
		//var_dump($all_items.'cur'.$cur);
		if($cur*$this->page_items+$this->page_items<$all_items)
		{
			for($i=0;$i<$this->page_items;$i++)
			{
				$this->return_items[] = $this->all_items[$cur*$this->page_items+$i];
			}
		}
		else
		{
			for($i=$cur*$this->page_items;$i<$all_items;$i++)
			{
				$this->return_items[] = $this->all_items[$i];
			}
		}
		return $this->return_items;
	}
	
	/**
	 * 返回分页菜单
	 */
	function returnPageMenu()
	{
		$menu_array  = array();
		$menu_array[]='<a href="index.php?cur_page=1"><span class="pagelist"><<</span></a>';
		if($this->cur_page==1)
		{
			$menu_array[] ='<span class="pagelist"><</span>';
		}
		else
		{
			$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page-1).'"><span class="pagelist"><</span></a>';
		}
		
		//总的页数 小于= 5页
		if($this->all_pages<=5)
		{
			for($i=1;$i<=$this->all_pages;$i++)
			{
				if($i==$this->cur_page)
				{
					$menu_array[] ='<a href="index.php?cur_page='.$i.'"><span class="curpagelist">'.$i.'</span></a>';
				}
				else
				{
					$menu_array[] ='<a href="index.php?cur_page='.$i.'"><span class="pagelist">'.$i.'</span></a>';
				}
			}
			if($this->cur_page==$this->all_pages)
			{
				$menu_array[] ='<span class="pagelist">></span>';
			}
			else
			{
				$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+1).'"><span class="pagelist">></span></a>';
			}
			$menu_array[] ='<a href="index.php?cur_page='.$this->all_pages.'"><span class="pagelist">>></span></a>';
			return $menu_array;
		}
		
		/*大于5页*/
		//左边有两页右边 也有两页 
		if($this->cur_page>=3 && ($this->cur_page+2)<=$this->all_pages)
		{
			$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page-2).'"><span class="pagelist">'.($this->cur_page-2).'</span></a>';
			$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page-1).'"><span class="pagelist">'.($this->cur_page-1).'</span></a>';
			$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page  ).'"><span class="curpagelist">'.($this->cur_page  ).'</span></a>';
			$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+1).'"><span class="pagelist">'.($this->cur_page+1).'</span></a>';
			$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+2).'"><span class="pagelist">'.($this->cur_page+2).'</span></a>';
		}
		//左边有大于
		else if($this->cur_page+1==$this->all_pages )
		{
			for($i=-3;$i<2;$i++)
			{
				if($i==0)
				{
					$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+$i).'"><span class="curpagelist">'.($this->cur_page+$i).'</span></a>';
				}
				else
				{
					$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+$i).'"><span class="pagelist">'.($this->cur_page+$i).'</span></a>';
				}
				
			}
		}
		else if($this->cur_page==$this->all_pages)
		{
			for($i=-4;$i<1;$i++)
			{
				if($i==0)
				{
					$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+$i).'"><span class="curpagelist">'.($this->cur_page+$i).'</span></a>';
				}
				else
				{
					$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+$i).'"><span class="pagelist">'.($this->cur_page+$i).'</span></a>';
				}
				
			}
		}
		else if($this->cur_page==2)
		{
			for($i=-1;$i<4;$i++)
			{
				if($i==0)
				{
					$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+$i).'"><span class="curpagelist">'.($this->cur_page+$i).'</span></a>';
				}
				else
				{
					$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+$i).'"><span class="pagelist">'.($this->cur_page+$i).'</span></a>';
				}
				
			}
		}
		else if($this->cur_page==1)
		{
			for($i=0;$i<5;$i++)
			{
				if($i==0)
				{
					$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+$i).'"><span class="curpagelist">'.($this->cur_page+$i).'</span></a>';
				}
				else
				{
					$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+$i).'"><span class="pagelist">'.($this->cur_page+$i).'</span></a>';
				}
				
			}
		}
		
		if($this->cur_page==$this->all_pages)
		{
			$menu_array[] ='<span class="pagelist">></span>';
		}
		else
		{
			$menu_array[] ='<a href="index.php?cur_page='.($this->cur_page+1).'"><span class="pagelist">></span></a>';
		}
		$menu_array[] ='<a href="index.php?cur_page='.$this->all_pages.'"><span class="pagelist">>></span></a>';
		return $menu_array;
	}
}
?>